package com.dk.controller;

import com.dk.pojo.Menu;
import com.dk.pojo.playerData.PlayerData;
import com.dk.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 后台功能管理
 */
@Controller
@RequestMapping("menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @RequestMapping("show")
    public String show(){
        return "/ajax/home";
    }

    @RequestMapping("toShow")
    public String toShow(){
        return "/menu/show";
    }

    /**
     * 根据不同的用户账号展示不同的权限
     * @param request
     * @return
     */
    @RequestMapping("getMenuInfo")
    @ResponseBody
    public List<Map<String,Object>> getMenuInfo(HttpServletRequest request){
        PlayerData playerData = (PlayerData) request.getSession().getAttribute("player");
        return menuService.getMenuInfo(playerData.getPlayerAccount());
    }

    /**
     * 树状列表展示
     * @return
     */
    @RequestMapping("getZtreeMenu")
    @ResponseBody
    public List<Map<String,Object>> getZtreeMenu(){
        return menuService.getZtreeMenu();
    }

    @RequestMapping("toAdd")
    public String toAdd(){
        return "/menu/add";
    }

    @RequestMapping("add")
    @ResponseBody
    public boolean add(Menu menu){
        return menuService.add(menu);
    }

    @RequestMapping("toUpdate/{id}")
    public String toUpdate(@PathVariable("id")Integer menuId, Model model){
        Menu menu = menuService.toUpdate(menuId);
        model.addAttribute("menu",menu);
        return "/menu/update";
    }

    @RequestMapping("deleteById/{id}")
    @ResponseBody
    public boolean deleteById(@PathVariable("id")Integer menuId){
        return menuService.deleteById(menuId);
    }

    @RequestMapping("update")
    @ResponseBody
    public boolean update(Menu menu){
        return menuService.update(menu);
    }

    @RequestMapping("getZtreeMenuChecked/{positionId}")
    @ResponseBody
    public List<Map<String,Object>> getZtreeMenuChecked(@PathVariable("positionId")Integer positionId){
        List<Map<String,Object>> list = menuService.getZtreeMenuChecked(positionId);
        return list;
    }

}
